package thirdassigment;

import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Copy implements ICommandExecutor{
	private static ICommandExecutor instance;
public void execute(String enterstr) throws IOException{
	
	/*String[] StrArray = enterstr.split(" ");
	String file1 = StrArray[1];
	String file2 = StrArray[2];
	
    FileInputStream f1 = new FileInputStream(file1);
	FileOutputStream f2 = new FileOutputStream(file2);
	
			fileCopy(f1, f2);
	*/
	String[] StrArray = enterstr.split(" ");
	String file1 = StrArray[1];
	String file2 = StrArray[2];
	File s = new File(file1);
	File d = new File(file2 + "\\" +s.getName());
	
	copyFile(s,d);
}

public static void copyFile(File source, File dest) {
    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = new FileInputStream(source);
        out = new FileOutputStream(dest);
        
        int nLength;
        byte[] buf = new byte[8000];
        while (true) {
            nLength = in.read(buf);
            if (nLength < 0) {
                break;
            }
            out.write(buf, 0, nLength);
        }
       
    } catch (IOException ex) {
         
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception ex) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception ex) {
            }
        }
    }
   
}
/*public static void fileCopy(InputStream in, OutputStream out) throws IOException
		{
		  int nLength;
		  byte[] buf = new byte[8000];
		  while(true)
		  {
		    nLength = in.read(buf);
		      if(nLength < 0){ 
		        break;
		      }
		    out.write(buf, 0, nLength);
		  }
		       
		  in.close();
		  out.close();
		}

*/
private Copy(){          
}
public static ICommandExecutor getInstance() {
    if(instance==null){
            instance=new Copy();       
            }
    return instance;
}
}
